package com.xtool.livewallpaper.video

import android.app.Activity
import android.app.WallpaperManager
import android.content.ComponentName
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.core.view.isVisible
import com.gyf.immersionbar.ImmersionBar
import com.ricky.enavigation.ENavigation
import com.xtool.livewallpaper.R
import com.xtool.livewallpaper.WallpaperNotifyManager
import com.xtool.livewallpaper.base.BaseActivity
import com.xtool.livewallpaper.base.PrefsHelper
import com.xtool.livewallpaper.util.SystemRouterUtil
import com.xtool.livewallpaper.util.ToastUtil
import com.xtool.livewallpaper.base.gone
import com.xtool.livewallpaper.databinding.ActivityVideoWallpaperBinding


class VideoWallpaperActivity : BaseActivity() {
    companion object {
        const val VIDEO_URI = "video_uri"
    }

    private val viewModel: VideoWallpaperViewModel by viewModels()

    private val binding: ActivityVideoWallpaperBinding by lazy {
        ActivityVideoWallpaperBinding.inflate(
            layoutInflater
        )
    }

    override fun onStart() {
        super.onStart()
        ImmersionBar.with(this)
            .transparentStatusBar()
            .init()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel.videoPath = Uri.parse(intent.getStringExtra(VIDEO_URI))
        setContentView(binding.root)
        binding.btnBack.setOnClickListener { finish() }
        WallpaperNotifyManager.instance.connectWallpaperProcess()
        binding.btnSet.setOnClickListener {
            SystemRouterUtil.runWithAlertPermission(
                this,
                getString(R.string.open_alert_permission)
            ) {
                setWallpaper()
            }
        }
        binding.playerView.setOnClickListener {
            if (viewModel.mediaPlayer.isPlaying()) {
                viewModel.mediaPlayer.pause()
                binding.btnPlay.visibility = View.VISIBLE
            } else {
                viewModel.mediaPlayer.start()
                binding.btnPlay.visibility = View.GONE
            }
        }
        binding.btnMute.setOnClickListener {
            binding.btnMute.isSelected = !binding.btnMute.isSelected
            viewModel.mediaPlayer.setMute(binding.btnMute.isSelected)
        }
        binding.btnMute.isSelected = PrefsHelper.isMute
        binding.playerView.init(viewModel.mediaPlayer, viewModel.videoPath!!)
        binding.playerView.setVideoPath(viewModel.videoPath!!)
        viewModel.mediaPlayer.setMute(binding.btnMute.isSelected)
        binding.btnPlay.gone()
    }

    private fun setWallpaper() {
        val path = viewModel.videoPath ?: return
        val result = WallpaperNotifyManager.instance.setVideoWallpaper(
            path,
            binding.btnMute.isSelected
        )
        if (!result) {
            ToastUtil.show(R.string.invalid_video)
            return
        }
        ENavigation.with(this)
            .setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER)
            .putParcelable(
                WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
                ComponentName(packageName, (VideoWallpaperService::class.java.canonicalName))
            )
            .putBoolean("isMute", binding.btnMute.isSelected)
            .onResult { _, resultCode, _ ->
                if (resultCode == Activity.RESULT_OK) {
                    finish()
                    ToastUtil.show(R.string.set_wallpaper_success)
                    SystemRouterUtil.goHome()
                }
            }.navigate()

    }

    override fun onPause() {
        super.onPause()
        binding.playerView.pause()
    }

    override fun onResume() {
        super.onResume()
        if (!binding.btnPlay.isVisible) binding.playerView.start()
    }

    override fun onDestroy() {
        viewModel.mediaPlayer.release()
        super.onDestroy()
    }

    override fun finish() {
        viewModel.mediaPlayer.stop()
        binding.btnBack.gone()
        binding.btnMute.gone()
        super.finish()
        overridePendingTransition(0, R.anim.activity_slide_out)
    }

}